﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OUSMS.AppLogic.BusinessLogicLayer;
using OUSMS.Helpers;
using System.Web.ModelBinding;
using OUSMS.AppLogic.DataAccessLayer;
namespace OUSMS
{
    public partial class RegisterUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                departmentDDL.DataSource = GetAllDepartments().ToList();
                departmentDDL.DataBind();

                SecondDepartmentDDL.DataSource = GetSecondDepartmentsByDepartmentID(Convert.ToInt32(departmentDDL.SelectedValue)).ToList();
                SecondDepartmentDDL.DataBind();
            }
        }

        public IQueryable<Titles> GetAllTitles()
        {
            TitlesBLL titlesBLL = new TitlesBLL();
            return titlesBLL.GetAllTitles();
        }

        public IQueryable<StudyAreas> GetALLStuyAreas()
        {
            StudyAreasBLL studyAreaBLL = new StudyAreasBLL();
            return studyAreaBLL.GetALLStuyAreas();
        }

        public IQueryable<Departments> GetAllDepartments()
        {
            DepartmentsBLL departmentsBLL = new DepartmentsBLL();
            return departmentsBLL.GetAllDepartments();
        }

        public IQueryable<SecondDepartments> GetSecondDepartmentsByDepartmentID(int departmentID)
        {
            SecondDepartmentsBLL secondDepartmentBLL = new SecondDepartmentsBLL();
            return secondDepartmentBLL.GetSecondDepartmentsByDepartmentID(departmentID);
        }

        protected void departmentDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            SecondDepartmentDDL.DataSource = GetSecondDepartmentsByDepartmentID(Convert.ToInt32(departmentDDL.SelectedValue)).ToList();
            SecondDepartmentDDL.DataBind();
        }

        protected void okButton_Click(object sender, EventArgs e)
        {
            try
            {
                UsersBLL userBLL = new UsersBLL();
                errorMsgLbl.Visible = true;
                if (userBLL.GetUserByLoginName(loginNameTxtBox.Text.Trim()) == null)
                {
                    Users registeredUser = new Users();
                    registeredUser.LoginName = loginNameTxtBox.Text.Trim();
                    registeredUser.Password = passwordTxtBox.Text.Trim();
                    registeredUser.Name = nameTxtBox.Text.Trim();
                    registeredUser.Email = emailTxtBox.Text.Trim();
                    registeredUser.Gender = genderDDL.SelectedValue;
                    registeredUser.Phone = phoneTxtBox.Text.Trim();
                    registeredUser.DepartmentID = Convert.ToInt32(departmentDDL.SelectedValue);
                    registeredUser.SecondDepartmentID = Convert.ToInt32(SecondDepartmentDDL.SelectedValue);
                    registeredUser.Position = positionTxtBox.Text.Trim();
                    registeredUser.TitleID = Convert.ToInt32(titleDDL.SelectedValue);
                    registeredUser.Introduction = introductionTxtBox.Text.Trim();
                    registeredUser.LastLogonDateTime = DateTime.Now;
                    registeredUser = userBLL.RegisterUser(registeredUser);

                    userBLL.ClearUserStudyAreas(registeredUser);
                    for (int index = 0; index < studyAreaChkList.Items.Count; index++)
                    {
                        if (studyAreaChkList.Items[index].Selected)
                        {
                            UserStudyAreas userStudyArea = new UserStudyAreas();
                            userStudyArea.StudyAreaID = Convert.ToInt32(studyAreaChkList.Items[index].Value);
                            userStudyArea.UserID = registeredUser.ID;
                            userBLL.AddUserStudyArea(userStudyArea);
                        }
                    }
                    string emailBody = string.Format("{0}, 您好： 您的注册申请已成功提交，我们将尽快进行审核，审核通过后您将可以进行科研提案的相关操作。", nameTxtBox.Text);
                    Emailer.SendMail("国家开放大学科研提案系统注册通知", emailTxtBox.Text.Trim(), "libncepu@sina.com", emailBody);

                    errorMsgLbl.Text = "您的注册申请已成功提交，请等待管理员审核。";
                }
                else
                {
                    errorMsgLbl.Text = "登录名已被其他用户占用，请您重现填写注册登录名。";
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                errorMsgLbl.Visible = true;
                errorMsgLbl.Text = string.Format("注册失败，请联系管理员，错误信息：{0}",ex.Message);
            }
        }
    }
}